#include <gtest/gtest.h>
#include "HixlCsClient.h"

// stub 计数
int GetEndpointCreateCount();
int GetEndpointDestroyCount();

TEST(HixlCsClientTest, CreateSuccess)
{
    hixl::HixlCsClient client;

    char ip[] = "127.0.0.1";

    hccl::EndPoint ep;
    ep.protocol = hccl::CommProtocol::TCP;
    ep.commAddr.type = hccl::CommAddrType::IP_V4;
    ep.commAddr.id = 0;

    int create_before = GetEndpointCreateCount();

    auto ret = client.Create(ip, 50000, &ep);
    EXPECT_EQ(ret, hccl::HcclResult::SUCCESS);

    // Create 内部应调用 EndpointStore::CreateEndpoint -> HcommEndPointCreate
    EXPECT_EQ(GetEndpointCreateCount(), create_before + 1);
}

TEST(HixlCsClientTest, CreateWithNullIpShouldFail)
{
    hixl::HixlCsClient client;

    hccl::EndPoint ep;
    ep.protocol = hccl::CommProtocol::TCP;
    ep.commAddr.type = hccl::CommAddrType::IP_V4;
    ep.commAddr.id = 0;

    auto ret = client.Create(nullptr, 50000, &ep);
    EXPECT_EQ(ret, hccl::HcclResult::E_PARA);
}

TEST(HixlCsClientTest, CreateWithReservedProtocolShouldFail)
{
    hixl::HixlCsClient client;

    char ip[] = "127.0.0.1";

    hccl::EndPoint ep;
    ep.protocol = hccl::CommProtocol::RESERVED;
    ep.commAddr.type = hccl::CommAddrType::IP_V4;
    ep.commAddr.id = 0;

    auto ret = client.Create(ip, 50000, &ep);
    EXPECT_EQ(ret, hccl::HcclResult::E_PARA);
}

TEST(HixlCsClientTest, DestroyAfterCreateShouldDestroyEndpoint)
{
    hixl::HixlCsClient client;

    char ip[] = "127.0.0.1";
    hccl::EndPoint ep;
    ep.protocol = hccl::CommProtocol::TCP;
    ep.commAddr.type = hccl::CommAddrType::IP_V4;
    ep.commAddr.id = 0;

    auto ret = client.Create(ip, 50000, &ep);
    ASSERT_EQ(ret, hccl::HcclResult::SUCCESS);

    int destroy_before = GetEndpointDestroyCount();

    // 目前 Destroy 只会调用 DisconnectInternal，
    // 如果你在 DisconnectInternal 里实现了 DestroyEndpoint(self_endpoint_handle_),
    // 这里 destroy_count 应该 +1
    ret = client.Destroy(&client); // 或者 Destroy(nullptr) 视你的签名而定
    EXPECT_EQ(ret, hccl::HcclResult::SUCCESS);

    // 如果 DisconnectInternal 内部在 self_endpoint_handle_ 非空时 DestroyEndpoint，
    // 计数就会加一。
    EXPECT_EQ(GetEndpointDestroyCount(), destroy_before + 1);
}
